#include <vector>
#include <string>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <list>
using namespace std;

// 二分查找
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] > target) right = mid - 1;
            else if (nums[mid] < target) left = mid + 1;
            else return mid;
        }
        return -1;
    }
};

// 在排序数组中找元素的第一个和最后一个位置
class Solution {
public:
    vector<int> searchRange(vector<int>& nums, int target) {
        // 判断边界情况
        if (nums.size() == 0) return {-1, -1};
        int begin = 0;// 记录左端点
        int left = 0, right = nums.size() - 1;
        // 1、找左端点
        while (left < right)
        {
            int mid = left + (right - left) / 2;// 向下取整
            if (nums[mid] < target) left = mid + 1;
            else right = mid;
        }
        // 2、判断是否有结果
        if (nums[left] != target) return {-1, -1};
        else begin = left;
        // 3、找右端点
        left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;// 向上取整
            if (nums[mid] > target) right = mid - 1;
            else left = mid;
        }
        return {begin, right};
    }
};

// 搜索插入位置
class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] < target) left = mid + 1;
            else if (nums[mid] > target) right = mid - 1;
            else return mid;
        }
        return left;
    }
};

// x的平方根
class Solution {
public:
    int mySqrt(int x) {
        if (x < 1) return 0;
        int left = 1, right = x;
        while (left < right)
        {
            long mid = left + (right - left + 1) / 2;
            if (mid * mid <= x) left = mid; 
            else right = mid - 1;
        }
        return left;
    }
};

// 山脉数组的峰顶索引
class Solution {
public:
    int peakIndexInMountainArray(vector<int>& arr) {
        int left = 1, right = arr.size() - 2;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (arr[mid] > arr[mid - 1]) left = mid;
            else right = mid - 1;
        }
        return left;
    }
};

// 寻找山峰
class Solution {
public:
    int findPeakElement(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left + 1) / 2;
            if (nums[mid] > nums[mid - 1]) left = mid;
            else right = mid - 1;
        }
        return left;
    }
};

// 寻找旋转排序数组中的最小值
class Solution {
public:
    int findMin(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (nums[mid] > nums.back()) left = mid + 1;
            else right = mid;
        }  
        return nums[left];
    }
};

// 点名
class Solution {
public:
    int takeAttendance(vector<int>& records) {
        int left = 0, right = records.size() - 1;
        while (left < right)
        {
            int mid = left + (right - left) / 2;
            if (records[mid] > mid) right = mid;
            else left = mid + 1;
        }
        return left == records[left] ? left + 1 : left;
    }
};